package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.dao.log.RuleRidePhotologDao;
import com.tbit.uqbike.client.dao.log.RuleRidelogDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.RideLog;
import com.tbit.uqbike.client.pojo.TerPosition;
import com.tbit.uqbike.client.pojo.newEntity.RuleRidePhotolog;
import com.tbit.uqbike.client.pojo.newEntity.RuleRidelog;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.HistoryService;
import com.tbit.uqbike.client.service.RideLogService;
import com.tbit.uqbike.client.service.RuleRidelogService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:57 2022/7/12
 */
@Service
public class RuleRidelogServiceImpl implements RuleRidelogService {

    @Autowired
    private RuleRidelogDao ruleRidelogDao;
    @Autowired
    private RuleRidePhotologDao ruleRidePhotologDao;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private CalibrateService calibrateService;

    @Override
    public void edit(RuleRidelog ruleRidelog) {
        ruleRidelogDao.edit(ruleRidelog);
    }

    @Override
    public List<RuleRidelog> query(Integer userId) {
        return ruleRidelogDao.query(userId);
    }

    @Override
    public List<RuleRidePhotolog> queryPhoto(Integer rideId) {
        return ruleRidePhotologDao.query(rideId);
    }

    @Override
    public RuleRidelog getByRideId(Map<String, Object> map, Integer rideId, Integer mapType) {
        RuleRidelog ruleRidelog = ruleRidelogDao.getByRideId(rideId);
        if (ruleRidelog == null) {
            throw new BaseException("-3053", "不存在订单数据");
        }
        RideLog rideLog = rideLogService.getByOrderNO(ruleRidelog.getOrderNo());
        if (rideLog == null) {
            throw new BaseException("-3053", "不存在订单数据");
        }
        Point point1 = calibrateService.calibrate(new Point(ruleRidelog.getLon(), ruleRidelog.getLat()), MapConstant.MAP_ORI, mapType);
        ruleRidelog.setLonC(point1.getX());
        ruleRidelog.setLatC(point1.getY());
        List<RuleRidePhotolog> photologs = queryPhoto(ruleRidelog.getRideId());
        map.put("photologs", photologs);
        if (rideLog.getEndTime() == null) {
            rideLog.setEndTime(DateTimeUtil.getNowTime());
        }
        List<TerPosition> terPositions = historyService.getByMachineNOAndTime(mapType, rideLog.getMachineNO(), rideLog.getStartTime(), rideLog.getEndTime());
        List<Point> pointList = new ArrayList<>();
        if (terPositions.size() > 0) {
            terPositions.stream().forEach(s -> {
                Point point = new Point();
                point.setX(s.getLonC());
                point.setY(s.getLatC());
                pointList.add(point);
            });
        }
        map.put("terPositions", pointList);
        return ruleRidelog;
    }

    @Override
    public List<RuleRidelog> queryRuleRideLog(Map<String, Object> map, String orderNO, Integer mapType) {
        List<RuleRidelog> ruleRidelogs = ruleRidelogDao.getByOrderNO(orderNO);
        if (ruleRidelogs.isEmpty()) {
            throw new BaseException("-3053", "不存在订单数据");
        }
        RideLog rideLog = rideLogService.getByOrderNO(orderNO);
        if (rideLog == null) {
            throw new BaseException("-3053", "不存在订单数据");
        }
        for (RuleRidelog ruleRidelog : ruleRidelogs) {
            List<RuleRidePhotolog> photologs = queryPhoto(ruleRidelog.getRideId());
            ruleRidelog.setPhotologs(photologs);
        }
        if (rideLog.getEndTime() == null) {
            rideLog.setEndTime(DateTimeUtil.getNowTime());
        }
        List<TerPosition> terPositions = historyService.getByMachineNOAndTime(mapType, rideLog.getMachineNO(), rideLog.getStartTime(), rideLog.getEndTime());
        List<Point> pointList = new ArrayList<>();
        if (terPositions.size() > 0) {
            terPositions.stream().forEach(s -> {
                Point point = new Point();
                point.setX(s.getLonC());
                point.setY(s.getLatC());
                pointList.add(point);
            });
        }
        map.put("terPositions", pointList);
        return ruleRidelogs;
    }

    @Override
    public int insert(String machineNO, String remark) {
        RuleRidePhotolog ruleRidePhotolog = new RuleRidePhotolog();
        ruleRidePhotolog.setAddTime(new Date());
        //添加类型：0系统自动，1运维端，2管理平台，3客户端
        ruleRidePhotolog.setType(3);
        ruleRidePhotolog.setRemark(remark);
        ruleRidePhotolog.setMachineNO(machineNO);
        ruleRidePhotologDao.insert(ruleRidePhotolog);
        //System.out.println(ruleRidePhotolog);
        return ruleRidePhotolog.getPhotoId();
    }
}
